"""
@Project:catering_takeout
@File:123.py
@Author:柴奇峰
@Date:9:51
"""
import random
# list = [1,5,7,8,4,67,3,2]



list=[]
for i in range(200000):
    list.append(random.randint(1,99999))

"""
冒泡排序
"""

def maopao(list):
    a = []
    for i in range(len(list)-1):
        for j in range(len(list)-i-1):
            if list[j] < list[j+1]:
                a = list[j]
                list[j] = list[j+1]
                list[j+1] = a


"""
选择排序
"""
def xuanze(list):
    for i in range(len(list)-1):
        min_index = i
        for j in range(i+1,len(list)):
            if list[min_index] > list[j]:
                min_index = j
        list[min_index],list[i] = list[i],list[min_index]


    print(list)


"""
插入排序
"""
def insertion_sort(list):
    for i in range(1,len(list)):
        current = list[i] # 当前需要插入的元素
        per_index = i-1  # 与当前元素比较的比较元素
        while per_index >=0 and list[per_index] > current:
            # 当比较元素大于当前元素则把比较元素后移
            list[per_index + 1 ] = list[per_index]
            # 往前选择下一个比较元素
            per_index -= 1
        # 当比较元素小于当前元素，则将当前元素插入在其后面
        list[per_index + 1] = current
    return list

# a = [1,8,36,7,4,95,217,38,5,9,51]
# b = insertion_sort(a)
# print(b)


"""
快速排序
"""
def quicksort(list):
    if not list:
        return list
    pivot = random.choice(list)
    print(pivot)
    equalList = []
    smallerList = []
    biggerList = []

    for i in list:
        if i <pivot:
            smallerList.append(i)
        elif i == pivot:
            equalList.append(i)
        else:
            biggerList.append(i)
    return quicksort(biggerList) + equalList + quicksort(smallerList)


"""
希尔排序
"""
def shell_sort(alist):
    # 取整计算增量（间隔）值
    gap = len(alist) //2
    while gap> 0 :
        for i in range(gap,len(alist)):
            j = i
            current = alist[i]
            # 元素与他同列的前面的每个元素比较，如果比前面第小则互换，
            while j - gap >= 0 and current < alist[j - gap]:
                alist[j] = alist[j-gap]
                j -= gap
            alist[j] = current
        # 缩小增量（间隔）值
        gap //= 2
    return alist
sorted_arr = shell_sort([3,1,8,9,2,6,0,8])
print(sorted_arr)

"""
归并排序
"""
def merge_sort(alist):
    #递归结束条件
    if len(alist) <=1:
        return alist

    # 分解决问题,平递归调用
    middle = len(alist) // 2
    left = merge_sort(alist[:middle])
    right = merge_sort(alist[middle:])

    # 合并左右部分，完成排序
    merged_list = []
    while left and right:
        if left[0] <= right[0]:
            merged_list.append(left.pop(0))
        else:
            merged_list.append(right.pop(0))

    # 如果左部分或右部分还有剩余，那就拼接到已经排好的列表中
    merged_list.extend(right if right else left)
    return merged_list


"""
计数排序
"""
def counting_sort(alist):
    largest = max(alist);smallest = min(alist) # 最大值，最小值
    counter = [0 for i in range(largest-smallest+1)] # 用于统计个数第空数组
    idx = 0 # 桶内索引值
    for i in range(len(alist)):
        counter[alist[i]-smallest] += 1 # 统计每个元素出现第次数
    for j in range(len(counter)):
        while counter[j] > 0:
            alist[idx] = j + smallest # 取出元素
            idx += 1
            counter[j] -= 1
    return alist


# a = [8,4,6,100,5,1,3,8,9,10]
# counting_sort(a)
# print(a)




"""
堆排序
"""
from heapq import heappop,heappush

def heaqpsort(alist):
    h = []

    for i in alist:
        heappush(h,i)

    return [heappop(h) for j in range(len(h))]

alist = [9,5,3,7,8,1,5,11]
lst = heaqpsort(alist)
print('堆排序',lst)





def outher(a):
    b=1024
    def inner():
        print(a+b)
    return inner()

wer = outher(2015)

import time

def tiasd(func):
    ang = time.time()
    func()
    end = time.time()
    print(f'执行{func}花了{int(end-ang)}秒')

def asdnow():
    print('now开始执行')
    time.sleep(1)

def asdwtow():
    print('tow开始执行')
    time.sleep(3)


tiasd(asdnow)
tiasd(asdwtow)


def timeit(func):
    def inner():
        start = time.time()
        func()
        end = time.time()
        print(f'{func}执行时间{int(end - start)}秒')
    return inner
@timeit
def hello_one():
    print('hello_one()开始运行了')
    time.sleep(1)

def hello_two():
    print('hello_two()开始运行了')
    time.sleep(3)

   # 重新定义 hello_one
hello_one()